#!/usr/bin/env Rscript
## This file runs BRS on all deterministic simulated data and the probabilistic
## Simulated data for J=5

set.seed(123)  # shouldn't matter, since we pass a seed to the BRS function, but just in case

i <- 1
ruleSet_BRS_pois_dgp1 <- list() # list of all list (one for each sample size) of rule sets of this dgp
for(j in 1:length(sampleSize)){ # Loop through all sample sizes
  # for each rep
  ruleSet_BRS_pois_dgp1[[j]] <- foreach (k=1:numSims) %dopar% {
    cat("i=", i, "j=", j, "; k=", k, " \n", sep="")
    
    data <- allData[[i]][[k]]
    X <- data[allIndices[[j]][[k]],-ncol(data)]
    Y <- data[allIndices[[j]][[k]], ncol(data)] # Last column is Y
    
    lambda <- 1
    nu <- getEta(sampleSize[j])  # note: BRS_0.0.0.9006 uses nu to refer to eta from the paper
    
    str <- ncol(X)
    alpha_1 <- a1/str; alpha_2 <- a2/str; beta_1 <- b1/str; beta_2 <- b2/str
    
    BRS(df=X, Y=Y,
        maxLen=maxLen, trainProp=train_prop,
        numIter=Niteration, numChain=Nchain,
        supp=supp, numMine=numMine,
        alpha_1=alpha_1, alpha_2=alpha_2,
        beta_1=beta_1, beta_2=beta_2,
        prior_type="poisson",
        alpha_l=NULL, beta_l=NULL,  # not used for the poisson version
        lambda=lambda, nu=nu,  # note: BRS_0.0.0.9006 uses nu to refer to eta from the paper
        bootstrap=F,
        print=T,
        seed=seeds[[i]][[j]][k])[[1]][[1]]
  }
}

i <- 2
ruleSet_BRS_pois_dgp2 <- list() # list of all list (one for each sample size) of rule sets of this dgp
for(j in 1:length(sampleSize)){ # Loop through all sample sizes
  # for each rep
  ruleSet_BRS_pois_dgp2[[j]] <- foreach (k=1:numSims) %dopar% {
    cat("i=", i, "j=", j, "; k=", k, " \n", sep="")
    
    data <- allData[[i]][[k]]
    X <- data[allIndices[[j]][[k]],-ncol(data)]
    Y <- data[allIndices[[j]][[k]], ncol(data)] # Last column is Y
    
    lambda <- 1
    nu <- getEta(sampleSize[j])  # note: BRS_0.0.0.9006 uses nu to refer to eta from the paper
    
    str <- ncol(X)
    alpha_1 <- a1/str; alpha_2 <- a2/str; beta_1 <- b1/str; beta_2 <- b2/str
    
    BRS(df=X, Y=Y,
        maxLen=maxLen, trainProp=train_prop,
        numIter=Niteration, numChain=Nchain,
        supp=supp, numMine=numMine,
        alpha_1=alpha_1, alpha_2=alpha_2,
        beta_1=beta_1, beta_2=beta_2,
        prior_type="poisson",
        alpha_l=NULL, beta_l=NULL,  # not used for the poisson version
        lambda=lambda, nu=nu,  # note: BRS_0.0.0.9006 uses nu to refer to eta from the paper
        bootstrap=F,
        print=F,
        seed=seeds[[i]][[j]][k])[[1]][[1]]
  }
}


i <- 3
ruleSet_BRS_pois_dgp3 <- list() # list of all list (one for each sample size) of rule sets of this dgp
for(j in 1:length(sampleSize)){ # Loop through all sample sizes
  # for each rep
  ruleSet_BRS_pois_dgp3[[j]] <- foreach (k=1:numSims) %dopar% {
    cat("i=", i, "j=", j, "; k=", k, " \n", sep="")
    
    data <- allData[[i]][[k]]
    X <- data[allIndices[[j]][[k]],-ncol(data)]
    Y <- data[allIndices[[j]][[k]], ncol(data)] # Last column is Y
    
    lambda <- 1
    nu <- getEta(sampleSize[j])  # note: BRS_0.0.0.9006 uses nu to refer to eta from the paper
    
    str <- ncol(X)
    alpha_1 <- a1/str; alpha_2 <- a2/str; beta_1 <- b1/str; beta_2 <- b2/str
    
    BRS(df=X, Y=Y,
        maxLen=maxLen, trainProp=train_prop,
        numIter=Niteration, numChain=Nchain,
        supp=supp, numMine=numMine,
        alpha_1=alpha_1, alpha_2=alpha_2,
        beta_1=beta_1, beta_2=beta_2,
        prior_type="poisson",
        alpha_l=NULL, beta_l=NULL,  # not used for the poisson version
        lambda=lambda, nu=nu,  # note: BRS_0.0.0.9006 uses nu to refer to eta from the paper
        bootstrap=F,
        print=F,
        seed=seeds[[i]][[j]][k])[[1]][[1]]
  }
}


i <- 5
ruleSet_BRS_pois_dgp5 <- list() # list of all list (one for each sample size) of rule sets of this dgp
for(j in 1:length(sampleSize)){ # Loop through all sample sizes
  # for each rep
  ruleSet_BRS_pois_dgp5[[j]] <- foreach (k=1:numSims) %dopar% {
    cat("i=", i, "j=", j, "; k=", k, " \n", sep="")
    
    data <- allData[[5]][[k]]
    X <- data[allIndices[[j]][[k]],-ncol(data)]
    Y <- data[allIndices[[j]][[k]], ncol(data)] # Last column is Y
    
    lambda <- 1
    nu <- getEta(sampleSize[j])  # note: BRS_0.0.0.9006 uses nu to refer to eta from the paper
    
    str <- ncol(X); alpha_1 <- a1/str; alpha_2 <- a2/str; beta_1 <- b1/str; beta_2 <- b2/str
    
    BRS(df=X, Y=Y,
        maxLen=maxLen, trainProp=train_prop,
        numIter=Niteration, numChain=Nchain,
        supp=supp, numMine=numMine,
        alpha_1=alpha_1, alpha_2=alpha_2,
        beta_1=beta_1, beta_2=beta_2,
        prior_type="poisson",
        alpha_l=NULL, beta_l=NULL,  # not used for the poisson version
        lambda=lambda, nu=nu,  # note: BRS_0.0.0.9006 uses nu to refer to eta from the paper
        bootstrap=F,
        print=F,
        seed=seeds[[i]][[j]][k])[[1]][[1]]
  }
}

save(ruleSet_BRS_pois_dgp1, ruleSet_BRS_pois_dgp2, 
     ruleSet_BRS_pois_dgp3, ruleSet_BRS_pois_dgp5, 
     file="sim/out/sim_out1-3.5.rda")